<!--
 * @Author: liqi
 * @Date: 2022-10-12 15:24:21
 * @LastEditors: liqi
 * @LastEditTime: 2022-10-15 17:08:43
 * @Description:
-->
<template>
  <!-- 资讯评论审核 -->
  <div>
    <!-- 第一部分  -->
    <el-form :inline="true" :model="form" class="demo-form-inline">
      <el-form-item>
        <el-button type="danger" @click="batchDelete">批量删除</el-button>
      </el-form-item>
      <el-form-item>
        <el-input
          v-model="form.keywords"
          placeholder="请输入关键字"
          clearable
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="toSearch">查询</el-button>
      </el-form-item>
    </el-form>
    <!-- 第一部分结束 -->
    <!-- 第二部分  -->
    <el-table
      ref="multipleTable"
      :data="tableData"
      tooltip-effect="dark"
      style="width: 100%"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" />
      <el-table-column
        label="序号"
        type="index"
        align="center"
      />
      <el-table-column
        prop="comment"
        label="评论内容"
        show-overflow-tooltip
        align="center"
      />
      <el-table-column prop="userId" label="评论人编号" align="center" />
      <el-table-column prop="articleId" label="资讯编号" align="center" />
      <el-table-column prop="commentTime" label="评论时间" align="center">
        <template slot-scope="scope">
          {{ scope.row.commentTime | fmtDate1 }}
        </template>
      </el-table-column>
      <el-table-column prop="status" label="评论状态" align="center">
        <template slot-scope="scope">
          <el-tag
            v-if="scope.row.status === '通过'"
            type="success"
          >通过</el-tag>
          <el-tag v-else type="danger">不通过</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="评论操作" align="center">
        <template slot-scope="scope">
          <a
            href="#"
            class="del"
            @click.prevent="toDelete(scope.row.id)"
          >删除</a>
        </template>
      </el-table-column>
    </el-table>
    <!-- 第二部分结束 -->
    <!-- 第三部分 分页按钮 -->
    <el-pagination
      :current-page="params.page"
      :page-sizes="[5, 6, 8, 10, 15, 20]"
      :page-size="params.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      class="page"
      align="right"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
    <!-- 第三部分结束 分页按钮 -->
  </div>
</template>

<script>
import { queryComment, deleteComment, batchDelete } from '@/api/infoComment.js'
import { log } from '@antv/g2plot/lib/utils'
export default {
  data() {
    return {
      params: {
        page: 1,
        pageSize: 10
      },
      form: {},
      tableData: [],
      ids: [],
      total: 0
    }
  },
  computed: {},
  created() {
    this.getTableData()
  },
  mounted() {},
  methods: {

    // 删除
    toDelete(id) {
      this.$confirm('此操作将永久删除该评论, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          const res = await deleteComment({ id })
          this.$message({
            type: 'success',
            message: res.message
          })
          this.getTableData()
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    // 查询
    toSearch() {
      this.params.page = 1
      this.getTableData()
    },
    // 获取表单数据
    async getTableData() {
      const temp = { ...this.params, ...this.form }
      for (const i in temp) {
        if (!temp[i]) {
          delete temp[i]
        }
      }
      const res = await queryComment(temp)
      this.tableData = res.data.list
      this.total = res.data.total
    },
    handleSelectionChange(val) {
      this.ids = val.map((item) => {
        return item.id
      })
    },
    // 批量删除
    batchDelete() {
      if (this.ids.length > 0) {
        this.$confirm('此操作将批量删除评论, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(async() => {
            const res = await batchDelete({ ids: this.ids.toString() })
            this.$message({
              type: 'success',
              message: res.message
            })
            this.getTableData()
          })
          .catch(() => {
            this.$message({
              type: 'info',
              message: '已取消删除'
            })
          })
      }
    },
    // 分页
    handleSizeChange(pageSize) {
      this.params.pageSize = pageSize
      this.params.page = 1
      this.getTableDate()
    },
    handleCurrentChange(page) {
      this.params.page = page
      this.getTableDate()
    }
  }
}
</script>
<style scoped>
.del {
  color: #ffba00;
}
.del:hover{
  color: black;
}
.page {
  margin-top: 10px;
}
</style>
